package cn.bd13.swagger.controller;

import cn.bd13.swagger.entity.ReturnResult;
import cn.bd13.swagger.entity.User;
import cn.bd13.swagger.service.Service;
import cn.bd13.swagger.utils.ReturnUtils;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * ClassName：UserController
 * Package:cn.bd13.swagger.controller
 * Description:
 *
 * @Date:2020/5/12 22:21
 * @Author:zhangqiang@kgc.cn
 */
@RestController
@Api(tags = "这是用户的功能")
public class UserController {
    @Resource
    private Service service;

    @ApiOperation(value = "这是用户登录的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" ,example = "zwj" ,required = true),
            @ApiImplicitParam(name = "password" ,example = "111" ,required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 400 ,message = "数据库没有返回值"),
            @ApiResponse(code = 501 ,message = "数据库操作有误"),
            @ApiResponse(code = 502 ,message = "JAVA操作有误"),
            @ApiResponse(code = 503 ,message = "REDIS操作有误")
    })
    @GetMapping("/loginUser/{name}/{password}")
    public ReturnResult loginUser(@PathVariable("name") String name,
                                  @PathVariable("password") String password){
        try {
            User user=service.login(name);
            if (user!=null){
                if (password.equals(user.getPassword())){
                    return ReturnUtils.returnSuccessWithData(user);
                }else {
                    return ReturnUtils.returnFailed(400);
                }
            }else {
                return ReturnUtils.returnFailed(400);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnUtils.returnFailed(501);
        }
    }

    @ApiOperation(value = "这是查询的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" ,example = "zwj" ,required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 400 ,message = "数据库没有返回值"),
            @ApiResponse(code = 501 ,message = "数据库操作有误"),
            @ApiResponse(code = 502 ,message = "JAVA操作有误"),
            @ApiResponse(code = 503 ,message = "REDIS操作有误")
    })
    @GetMapping("/queryUser/{name}")
    public ReturnResult queryUser(@PathVariable("name") String name) {
        try {
            User user = service.getUserName(name);
            if (user!=null){
                //正常查询出结果
                return ReturnUtils.returnSuccessWithData(user);
            }else {
                //没有查询出结果
                return ReturnUtils.returnFailed(400);
            }

        } catch (Exception e) {
            e.printStackTrace();
            //sql异常
            return ReturnUtils.returnFailed(501);
        }
    }

    @ApiOperation(value = "这是修改姓名的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" ,example = "zwj" ,required = true),
            @ApiImplicitParam(name = "id" ,example = "55" ,required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 400 ,message = "数据库没有返回值"),
            @ApiResponse(code = 501 ,message = "数据库操作有误"),
            @ApiResponse(code = 502 ,message = "JAVA操作有误"),
            @ApiResponse(code = 503 ,message = "REDIS操作有误")
    })
    @GetMapping("/modifyUser/{id}/{name}")
    public ReturnResult modifyUser(@PathVariable("name") String name,
                                  @PathVariable("id")String id) {
        try {
            int i = service.modifyUserName(id,name);
            if (i==1){
                //正常查询出结果
                return ReturnUtils.returnSuccessWithData(service.getUserName(name));
            }else {
                //没有查询出结果
                return ReturnUtils.returnFailed(400);
            }

        } catch (Exception e) {
            e.printStackTrace();
            //sql异常
            return ReturnUtils.returnFailed(501);
        }
    }
}
